C
      SUBROUTINE HASH (A, N, NPOP, IFEAT, NREC, NPIX, JFAC, IMOD, JMOD)
C
C     THE PROGRAM COMPUTES A 4 DIMENSIONAL HISTOGRAM.
C     THE HISTOGRAM IS USED TO COMPUTE THE MEAN, VARIANCE, AND ENTROPY.
C
C     THE VECTORS ARE READ IN ONE RECORD AT A TIME USING THE VARIABLE A.
C     N(I)=COMPONENTS OF THE I'TH VECTOR IN THE TABLE
C     NPOP(I)=THE NUMBER OF OCCURRENCES OF THE ITH VECTOR IN THE TABLE
C     IFEAT=MAXIMUM NUMBER OF DIFFERENT VECTORS ALLOWED
C     NREC=NUMBER OF RECORDS TO EXAMINE
C     NPIX=NUMBER OF PIXELS PER RECORD
C     JFAC=MULTIPLIER
C     IMOD=DIVISOR
C     JMOD=BASE
C     LM,IM=THE COMPONENTS OF A VECTOR (LOGICAL*1 AND INTEGER)
C     NBAND=NUMBER OF SPECTRAL IMAGES OR VECTOR DIMENSION
C     KNT=A VARIABLE USED TO COUNT THE NUMBER OF PICTURE ELEMENTS(PELS)
C     KOUNT=A VARIABLE USED TO COUNT THE NUMBER OF DIFFERENT VECTORS
C
C     ******************************************************************
C
      INTEGER A(NPIX)
      DIMENSION N(IFEAT), NPOP(IFEAT), NN(200), IH(128)
      LOGICAL*1 LM(4)
      EQUIVALENCE (IM, LM(1))
      DATA NBAND /4/
C
C     INITIALIZE ALL VECTOR POPULATIONS TO ZERO
      DO 83 I=1,IFEAT
   83 NPOP(I)=0
      KOUNT=0
      MFAC=100
      NLU = 0
      NSS = 0
      NMS = 0
      WRITE (6,750) NPIX, NREC, JFAC, IMOD, JMOD
      WRITE (6,710)
C
C     LOOP 300 READ IN NREC RECORDS
      DO 300 I=1,NREC
C
C     READ IN ONE RECORD
      READ (10) A
C
C     ACCUMULATE FOUR DIMENSIONAL HISTOGRAM FOR EACH RECORD
      DO 200 J=1,NPIX
C
C     GET EACH VECTOR OUT OF A AND PUT INTO M
      IM = A(J)
C
C     4 DIMENSIONAL HISTOGRAM ROUTINE (COMBINATION TABLE LOOK UP/SEARCH
C     PROCEDURE)
C
C     COMPUTE TABLE LOCATION FROM VECTOR COMPONENTS
      L=0
      DO 99 NB=1,NBAND
      NXX = LM(NB)
   99 L = L*JMOD + MOD(NXX,IMOD)
      L = JFAC*L + 1
      LL=L
C
C     CHECK FOR EMPTY TABLE LOCATION
  100 IF(NPOP(L).NE.0)GO TO 110
C
C     HAVE FOUND A NEW VECTOR, INCREMENT VECTOR COUNTER
      KOUNT=KOUNT+1
C
C     CHECK TO SEE IF LIMIT ON NUMBER OF VECTORS IS EXCEEDED
      IF (KOUNT.GT.IFEAT) GO TO 400
C
C     SET POPULATION OF NEW VECTOR TO ONE
      NPOP(L)=1
C
C     PUT NEW VECTOR INTO TABLE
      N(L) = IM
      IF (LL.EQ.L) NLU = NLU + 1
C
C     PRINT PEL NUMBER FOR EVERY 100TH AND 1000TH VECTOR
      IF (MOD(KOUNT,MFAC).NE.0) GO TO 200
      KNT = NPIX*(I-1) + J
      X = FLOAT(KNT)/FLOAT(KOUNT)
      WRITE(6,800)I,KNT,KOUNT,X
      IF (KOUNT.GE.9900) MFAC = 1000
      GO TO 200
C
C     TABLE LOCATION IS FILLED,
C     CHECK TO SEE IF VECTOR IS IN TABLE
  110 IF (N(L).NE.IM) GO TO 130
C
C     VECTOR IS IN TABLE, INCREMENT POPULATION COUNTER
      NPOP(L)=NPOP(L)+1
      IF (LL.EQ.L) NSS = NSS + 1
      GO TO 200
C
C     VECTOR IS NOT THE SAME AS THE ONE WITH INDEX L
C     TRY THE NEXT INDEX
  130 L=L+1
      NMS = NMS + 1
C
C     CHECK TO SEE IF INDEX IS LARGER THAN END OF TABLE
C     IF SO, SET INDEX TO ONE AND START AT BEGINNING OF TABLE
      IF (L.GT.IFEAT) L = 1
      GO TO 100
C
C     RETURN TO NEXT VECTOR
  200 CONTINUE
C
C     RETURN TO NEXT RECORD
  300 CONTINUE
C
C     HAVE COMPLETED HISTOGRAM
C     PRINT LAST NEW VECTOR AND PEL NUMBER THAT OCCURRED
  400 I=I-1
      KNT = NPIX*(I-1) + J-1
      CNT = KNT
      COUNT=KOUNT
      X = CNT/COUNT
      WRITE (6,800) I, KNT, KOUNT, X
      WRITE (6,805) NLU, NSS, NMS
C
C     WRITE OUT FEATURE VECTORS THAT OCCUR AT LEAST 1000 TIMES
      NVEC = 0
      DO 450 I=1,IFEAT
      IF (NPOP(I).LT.1000)GO TO 450
      IM = N(I)
      NVEC = NVEC + 1
      IF (NVEC.EQ.1) WRITE (6,810)
      IF (MOD(NVEC,2).EQ.1) WRITE (6,811) LM, NPOP(I)
      IF (MOD(NVEC,2).EQ.0) WRITE (6,812) LM, NPOP(I)
  450 CONTINUE
C
C     POPULATION DISTRIBUTION IN LOGARITHMIC INCREMENTS
      DO 500 I=1,200
  500 NN(I)=0
C
      DO 550 I=1,IFEAT
      IF (NPOP(I).EQ.0) GO TO 550
C
C     COUNT THE NUMBER OF VECTORS THAT OCCUR 1000'S OF TIMES
      II=NPOP(I)/1000
      IF(II.LT.1)GO TO 510
      II=II+108
      GO TO 540
C
C     COUNT THE NUMBER OF VECTORS THAT OCCUR 100'S OF TIMES
  510 II=NPOP(I)/100
      IF (II.LT.1)GO TO 530
      II=II+99
      GO TO 540
C
C     COUNT THE NUMBER OF VECTORS THAT OCCUR FROM 1 TO 99 TIMES
  530 II=NPOP(I)
  540 NN(II)=NN(II)+1
  550 CONTINUE
C
C     PRINT THE NUMBER OF VECTORS THAT OCCUR 1-99 TIMES, 100'S AND
C     1000'S OF TIMES
      WRITE (6,815)
      J = 0
      INC = 1
      DO 560 I=1,200
      IF (I.EQ.101) INC = 100
      IF (I.EQ.110) INC = 1000
      J = J + INC
      IF (NN(I).EQ.0) GO TO 560
      X=NN(I)*100/COUNT
      WRITE (6,820) J, NN(I), X
  560 CONTINUE
C
C     COMPUTE THE MEAN, VARIANCE AND ENTROPY FROM THE HISTOGRAM
      ALN2 = ALOG10(2.0)
      DO 610 NB=1,NBAND
      NXM = 0
      NXV = 0
      SUM = 0.0
      DO 585 I=1,128
  585 IH(I) = 0
      DO 600 I=1,IFEAT
      IF (NPOP(I).EQ.0) GO TO 600
      IM = N(I)
      NXX = LM(NB)
      NXM = NXM + NPOP(I)*NXX
      NXV = NXV + NPOP(I)*NXX**2
      IH(NXX+1) = IH(NXX+1) + NPOP(I)
  600 CONTINUE
      XMEAN = NXM / CNT
      SIGMA = (NXV-CNT*XMEAN**2) / (CNT-1.0)
      DO 605 I=1,128
      IF (IH(I).EQ.0) GO TO 605
      PROBX = IH(I) / CNT
      SUM = SUM + PROBX*ALOG10(PROBX)/ALN2
  605 CONTINUE
      ENTRPY = -SUM
  610 WRITE (6,830) NB, XMEAN, SIGMA, ENTRPY
      RETURN
C
  710 FORMAT (//15X,'SCAN NO.',10X,'PIXEL NO.',10X,'VECTOR NO.',10X,
     .'P/V RATIO'/)
  750 FORMAT ('1 PIXELS USED =',I5,5X,'RECORDS USED =',I5,5X,'MULTIPLIER
     . =',I3,5X,'DIVISOR =',I3,5X,'BASE =',I3)
  800 FORMAT (3I20,F20.4)
  805 FORMAT (/'  LOOK UPS =',I7,'   SINGLE SEARCHES =',I8,'   MULTIPLE
     .SEARCHES =',I8///)
  810 FORMAT ('1',20X,'VECTORS WITH POPULATIONS OF AT LEAST 1000'//)
  811 FORMAT (10X,4I4,I10)
  812 FORMAT ('+',50X,4I4,I10)
  815 FORMAT ('1',10X,'NO. OF TIMES'/8X,'A VECTOR OCCURRED',10X,'NO. OF
     .VECTORS',10X,'PERCENT OF TOTAL'/)
  820 FORMAT (I20,I25,F25.4)
  830 FORMAT (/10X,'BAND',I3,5X,'MEAN =',F8.3,5X,'VARIANCE =',F8.3,5X,
     .'ENTROPY =',F8.3)
      END
